﻿using System;
using System.Text;
using System.Collections.Generic;
using UT_AMS.DataAccess.BO;
using AMS.DataAccess.Sql;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UT_AMS.DataAccess
{
    /// <summary>
    /// Summary description for UT_SqlUpdate
    /// </summary>
    [TestClass]
    public class UT_SqlUpdate_Compact
    {
        public UT_SqlUpdate_Compact()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void TestUpdateTable()
        {
            UserInfo user = new UserInfo();
            SqlUpdate update = new SqlUpdate("RealTableName");
            update.CompactMode = true;
            update.AddBusinessObject(user);

            string sql = update.ToString();
            Assert.IsTrue(sql.IndexOf("RealTableName") > 0);
            Assert.IsTrue(sql.IndexOf("\r\n") == -1);

            update = new SqlUpdate("RealTableName", true);
            update.AddBusinessObject(user);

            sql = update.ToString();
            Assert.IsTrue(sql.IndexOf("RealTableName") > 0);
            Assert.IsTrue(sql.IndexOf("\r\n") == -1);


            update = new SqlUpdate("RealTableName");
            update.AddBusinessObject(user);
            update.CompactMode = true;

            sql = update.ToString();
            Assert.IsTrue(sql.IndexOf("RealTableName") > 0);
            Assert.IsTrue(sql.IndexOf("\r\n") > -1);
        }

        [TestMethod]
        public void TestUpdateBasic()
        {
            UserInfo user = new UserInfo();
            user.Name = "xiaoming";
            user.Birthday = DateTime.Parse("1984-5-9");
            user.GuidField = Guid.NewGuid();

            SqlUpdate update = new SqlUpdate("RealUserTable");
            update.AddBusinessObject(user);

            string sql = update.ToString();
            Assert.IsTrue(sql.IndexOf("BooksCount") > 0);
            Assert.IsTrue(sql.IndexOf("'xiaoming'") > 0);
            Assert.IsTrue(sql.IndexOf("'1984-05-09") > 0);
            Assert.IsTrue(sql.IndexOf("GuidField") > 0);
            Assert.IsTrue(sql.IndexOf("CommandUT") > 0);
        }

        [TestMethod]
        public void TestUpdatePKFieldsNotIncluded()
        {
            UserInfo user = new UserInfo();
            user.Name = "xiaoming";
            user.Birthday = DateTime.Parse("1984-5-9");
            user.GuidField = Guid.NewGuid();

            SqlUpdate update = new SqlUpdate("RealUserTable");
            update.AddBusinessObject(user);

            string sql = update.ToString();
            Assert.IsFalse(sql.IndexOf("AID") > 0);
        }

        [TestMethod]
        public void TestUpdateNullIntValue()
        {
            UserInfo user = new UserInfo();
            SqlUpdate update = new SqlUpdate("RealUserTable");
            update.AddBusinessObject(user);

            string sql = update.ToString();
            Assert.IsTrue(sql.IndexOf("Age = NULL") > 0);

            user.Age = 12;
            update = new SqlUpdate("RealUserTable");
            update.AddBusinessObject(user);

            sql = update.ToString();
            Assert.IsTrue(sql.IndexOf("Age = 12") > 0);
        }

        [TestMethod]
        public void TestUpdateNullGuidValue()
        {
            UserInfo user = new UserInfo();
            SqlUpdate update = new SqlUpdate("RealUserTable");
            update.AddBusinessObject(user);

            string sql = update.ToString();
            Assert.IsTrue(sql.IndexOf("GuidNullableField = NULL") > 0);

            user.GuidNullableField = Guid.NewGuid();
            update = new SqlUpdate("RealUserTable");
            update.AddBusinessObject(user);

            sql = update.ToString();
            Assert.IsFalse(sql.IndexOf("GuidNullableField = NULL") >= 0);
        }

        [TestMethod]
        public void TestUpdateNullEnumValue()
        {
            UserInfo user = new UserInfo();
            SqlUpdate update = new SqlUpdate("RealUserTable");
            update.AddBusinessObject(user);

            string sql = update.ToString();
            Assert.IsTrue(sql.IndexOf("UserType2 = NULL") > 0);

            user.UserType2 = UserType.Person;
            update = new SqlUpdate("RealUserTable");
            update.AddBusinessObject(user);

            sql = update.ToString();
            Assert.IsTrue(sql.IndexOf("UserType2 = 1") > 0);
        }

        [TestMethod]
        public void TestUpdateSpecialCharInString()
        {
            UserInfo user = new UserInfo();
            user.Name = "abcdefg'hijkl";

            SqlUpdate sql = new SqlUpdate("RealUserTable");
            sql.AddBusinessObject(user);

            string ssql = sql.ToString();
            Assert.IsTrue(ssql.IndexOf("'abcdefg''hijkl'") > 0);
        }

        [TestMethod]
        public void TestUpdateEmptyString()
        {
            UserInfo user = new UserInfo();
            SqlUpdate sql = new SqlUpdate("RealUserTable");
            sql.AddBusinessObject(user);

            string ssql = sql.ToString();
            Assert.IsTrue(ssql.IndexOf("Name = NULL") > 0);


            user = new UserInfo();
            user.Name = string.Empty;
            sql = new SqlUpdate("RealUserTable");
            sql.AddBusinessObject(user);

            ssql = sql.ToString();
            Assert.IsTrue(ssql.IndexOf("Name = ''") > 0);
        }
    }
}
